<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true"  label-width="68px">
      <el-form-item label="资产名称" prop="title">
        <el-input v-model.trim="queryParams.title" placeholder="请输入资产名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="使用人" prop="receivePeople">
        <el-input v-model.trim="queryParams.receivePeople" placeholder="请输入使用人" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleGet"
          >领用</el-button
        >
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="personalAssetsList">
      <el-table-column label="资产名称" align="center" prop="title" />
      <el-table-column label="使用人" align="center" prop="receivePeople" />
      <el-table-column label="使用地点" align="center" prop="useAddress" />
      <!-- 补充占位字段 -->
      <el-table-column label="资产编号" align="center" prop="test1" />
      <el-table-column label="领用日期" align="center" prop="test2" />
      <el-table-column label="规格型号" align="center" prop="test3" />
      <el-table-column label="归还状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ scope.row.status == 0? '待确认':'已归还' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="资产图片" align="center" prop="assetImage">
        <template slot-scope="scope">
          <el-image
            style="width: 120px; height: 60px"
            :src="scope.row.assetImage"
            :preview-src-list="[scope.row.assetImage]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text"
              @click="handleGuiHuan(scope.row)">归还</el-button>
          </template>
        </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="personageListFun"
    />
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="使用设备" prop="assetId">
          <el-select
            v-model="form.assetId"
            placeholder="请选择使用设备"
            clearable
            size="small"
          >
            <el-option
              v-for="(item, index) in assetList"
              :key="index"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="使用地址" prop="useAddress">
          <el-input v-model="form.useAddress" placeholder="请输入使用地址" />
        </el-form-item>
        <!-- 补充字段 -->
        <el-form-item label="资产编号" prop="test1">
          <el-input v-model="form.test1" placeholder="请输入资产编号" />
        </el-form-item>
        <el-form-item label="领用日期" prop="test2">
          <el-input v-model="form.test2" placeholder="请输入领用日期" />
        </el-form-item>
        <el-form-item label="规格型号" prop="test3">
          <el-input v-model="form.test3" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="备注说明" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  personageList,
  assetSelect,
  personageuse,
  giveBack
} from "@/api/amgSystem/personalAssets/index";

export default {
  name: "personalAssets",
  data() {
    return {
      loading: false,
      personalAssetsList: [],
      total: 0,
      assetList: [],
      form: {
        remark: "",
        assetId: null,
        useAddress: null,
      },
      open: false,
      title: "领用明细",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title:'',
        receivePeople:''
      },
      rules: {
        assetId: [
          { required: true, message: "领用人不能为空", trigger: "blur" },
        ],
        useAddress: [
          { required: true, message: "使用地址不能为空", trigger: "blur" },
        ],
        remark: [{ required: true, message: "备注不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.personageListFun();
    this.assetSelectFun();
  },
  methods: {
    // 归还
    async handleGuiHuan(row) {
      const res =await giveBack(row)
      if(res.code == 200){
        this.$message.success('归还成功')
        this.personageListFun();
      }
    },
    // 个人资产列表
    personageListFun() {
      this.loading = true;
      personageList(this.queryParams).then((res) => {
        console.log(res);
        res.rows.forEach(item => {
          item.status = 0
        })
        this.personalAssetsList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    // 闲置下拉
    assetSelectFun() {
      assetSelect().then((res) => {
        res.data.map((item) => {
          this.assetList.push({
            title: item.title,
            id: item.id,
          });
        });
      });
    },
    // 领用
    handleGet(row) {
      this.form = {
        remark: "",
        assetId: null,
        useAddress: null,
      };
      this.open = true;
    },
    // 确定
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          personageuse(this.form).then((res) => {
            this.msgSuccess("领用成功");
            this.open = false;
            this.personageListFun();
          });
        }
      });
    },
    // 取消
    cancel() {
      this.form = {
        remark: "",
        assetId: null,
        useAddress: null,
      };
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.personageListFun();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
